package trucaso;
/*
 * Class to connect to a server and play as a client.
 */


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;


public class RemoteClient extends Client implements Runnable {
	protected ChatPane chatPane;
	protected int serverPort;
	protected String serverName;
	protected Thread clientThread;


	protected RemoteClient(Deck d, CardZone c,Actions a,ScoreBoard sb, ChatPane cp,String sn,int p) {
		super(d,c,a,sb,cp);
		chatPane = cp;
		serverPort = p;
		serverName = sn;
		name = "Client";
		name2 = "Server";
		turn = true;
		
		actions.maso.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				maso();
			}
		});
		
		chatPane.putMessage("Establishing connection. Please wait ...");
		
	      try{  
	    	  socket = new Socket(serverName, serverPort);
	          chatPane.putMessage("Connected: " + socket);
	          scores.setNames(name,name2);
	          open();
	      }catch(UnknownHostException uhe){  
	    	  chatPane.putMessage("Host unknown: " + uhe.getMessage());
	      }catch(IOException ioe){  
	    	  chatPane.putMessage("Unexpected exception: " + ioe.getMessage());
	      }
	    clientThread = new Thread(this);
		chatPane.setOutputStream(streamOut);
		
		clientThread.start();
	}
	
	public void run() {
		sendVersion();
        while(true){  
        	try{  
        		String line = streamIn.readUTF();
        		if(line.length()>0) {
        			char c = line.charAt(0);
        			switch(c) {
        			case '<':chatPane.putMessage(line);break;
        			case 'h':receiveBothHands(line);
        					actions.resetButtons();
   	  						cardZone.setCards();
   	  						cardZone.repaint();
   	  						break;
        			case 'p':if(both[Integer.parseInt(line.substring(2))].isInGame()) {
	  							cardZone.playCard(Integer.parseInt(line.substring(2)),true);
	  							turn=!turn;
	  						}
	  						if(isFinished()) {
	  							try{
	  								streamOut.writeUTF("f");
	  								streamOut.flush();
	  							}catch(IOException ioex) {
	  								chatPane.putMessage("Error while sending cards data! ");
	  							}
	  						
	  						}
	  						break;
        			case 'n':receiveName(line);break;
        			case 's':receiveScores(line);
        			case 'm':receiveMaso();break;
        			case 'v':checkVersion(line);break;
        			case 't':turn=true;
	  				default: chatPane.putMessage("Received a Weird message");break;
        			}
        		}

           }catch(IOException ioe){  
           		chatPane.putMessage("The server was shut down and you got disconnected... Congratulations!");
           		try{
           			close();
           		}catch(IOException iox) {
           			chatPane.putMessage("Error While Closing! "+iox.getMessage());
           		}
           		break;
           }
		}
	}
	
	protected synchronized void receiveBothHands(String hands) {
		if(both[0]!=null) returnCards();
		for(int i=0;i<6;i++) {

			if(i<3) {
				both[i+3] = deck.getCard(Integer.parseInt(hands.substring(2+2*i,4+2*i)));
				both[i+3].setInGame(true);
				both[i+3].setVisible(true);
			}else {
				both[i-3] = deck.getCard(Integer.parseInt(hands.substring(2+2*i,4+2*i)));
				both[i-3].setInGame(true);
			}
		}
	}
	
	protected void receiveScores(String sc) {
		int server,client;
		server = Integer.parseInt(sc.substring(2,5));
		client = Integer.parseInt(sc.substring(5,7));
		scores.setScores(client,server);
	}
	
	
}
